package com.youxue.teachin.set.child.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.library.rest.ApiServiceManager;
import com.library.rest.rxjava.RxResultHelper;
import com.library.rest.rxjava.RxSchedulersHelper;
import com.library.uiframework.fragment.BaseFragment;
import com.library.uiframework.utils.MToast;
import com.library.uiframework.utils.PatternUtils;
import com.library.uiframework.utils.StringUtils;
import com.youxue.teachin.R;
import com.youxue.teachin.common.utils.DefaultSubscriber;
import com.youxue.teachin.common.vo.UserVO;
import com.youxue.teachin.user.repository.IUserRepository;
import com.youxue.teachin.user.userSystem.UserSystem;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-06 23:33
 * @version: 9.1.0
 */
public class ChangePhoneFragment extends BaseFragment implements IChageUserView, OnClickListener {

    UserChangePresener mPresenter;
    private View mRootView;
    private EditText mCurPhone;
    private EditText mNewPhone;
    private EditText mLoginInputCaptcha;
    private TextView mGetCaptcha;
    private TextView mRegistBt;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.set_change_phone_fragment, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initView(View rootView) {

        mPresenter = new UserChangePresener(this);
        mCurPhone = (EditText)rootView.findViewById(R.id.cur_phone);
        mNewPhone = (EditText)rootView.findViewById(R.id.new_phone);
        mLoginInputCaptcha = (EditText)rootView.findViewById(R.id.login_input_captcha);
        mGetCaptcha = (TextView)rootView.findViewById(R.id.get_captcha);
        mRegistBt = (TextView)rootView.findViewById(R.id.regist_bt);

        mCurPhone.setEnabled(false);
        String currentPhone = getParams().getString("phone", "");
        mCurPhone.setText(currentPhone);


        mRegistBt.setOnClickListener(this);
        mGetCaptcha.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hideInputMethod();
    }

    private void getCaptcha() {
        String phone = mCurPhone.getText().toString();
        if (!PatternUtils.isMobile(phone)) {
            MToast.getInstance().setText("手机号码输入不正确");
            return;
        }

        Class<IUserRepository> clazz = IUserRepository.class;
        ApiServiceManager.getOrCreateApiService(clazz)
            .getCaptcha(phone, IUserRepository.CAPTCHA_UPD)
            .compose(RxResultHelper.<String>handleResult())
            .compose(RxSchedulersHelper.<String>io_main())
            .subscribe(new DefaultSubscriber<String>(){
                @Override
                public void onNext(String s) {
                    super.onNext(s);
                    MToast.getInstance().setText("验证码发送成功");
                }
            });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.get_captcha) {
            getCaptcha();
        } else {
            String phone = mNewPhone.getText().toString();
            if (!PatternUtils.isMobile(phone)) {
                MToast.getInstance().setText("手机号码输入不正确");
                return;
            }
            String captcha = mLoginInputCaptcha.getText().toString();
            if (StringUtils.isEmpty(captcha)) {
                MToast.getInstance().setText("请输入验证码！");
                return;
            }

            if (captcha.length() != 6) {
                MToast.getInstance().setText("输入的验证码位数不正确！");
                return;
            }

            mPresenter.changePhone(phone, captcha);
        }
    }

    @Override
    public void updateSucces() {
        MToast.getInstance().setText("更新手机号码成功");
        UserVO userVO = UserSystem.getInstance().getUserVO();
        userVO.phone = mNewPhone.getText().toString();
        finish();

    }

    @Override
    public void updateFail() {
        MToast.getInstance().setText("更新手机号码失败");
    }
}
